<?php

namespace App\Models;

class Areas extends Base
{
    protected $table = 'areas';

    const STATUS_1 = 1;
    const STATUS_2 = 2;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">隐藏</span>',
    ];

    protected $fillable = [
        'id','name','pid','sort','deep','region','status','created_at','updated_at'
    ];

    /**
     * 后台获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function getAdminList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('name','like','%' . $params['kwd'] . '%');
                }
            })->where('status','<>',-1)
            ->orderBy('sort','asc')->paginate($limit);
        $this->stateToText($lists,[
            'status' => self::STATUS_TEXT,
        ]);
        return $lists;
    }

    /**
     * 获取列表
     * @return mixed
     */
    protected function getList(){
        $lists = $this->where('status','<>',-1)->orderBy('sort','asc')->get()->toArray();
        $menus = $this->toFormatTree($lists,'name');
        return $menus;
    }

    /**
     * 获取级联列表
     * @return mixed
     */
    protected function getCascaderList(){
        $lists = $this->where('status','<>',-1)->orderBy('sort','asc')->get(['id as value','name as label','pid'])->toArray();
        return $this->listToTree($lists, 'value');
    }

    /**
     * 获取分类上级
     * @return mixed
     */
    protected function getPid($id=0){
        return $this->where('id',$id)->where('status','<>',-1)->value('pid');
    }

}
